package com.demo.common;

import com.demo.common.controller.BlogController;
import com.demo.common.controller.TestController;
import com.demo.common.model.Blog;
import com.demo.handler.FreeMarkerHandler;
import com.demo.handler.HtmlHandler;
import com.demo.interceptor.GlobalInterceptor;
import com.demo.interceptor.GlobalInterceptor1;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;

public class DemoConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		PropKit.use("config.properties");
		me.setDevMode(PropKit.getBoolean("devMode", false));
		me.setViewType(ViewType.JSP);
		me.setError404View("WEB-INF/view/404.html");
	}

	@Override
	public void configRoute(Routes me) {
		me.setBaseViewPath("WEB-INF/view");
		me.add("/blog", BlogController.class,"/blog");
		me.add("/test", TestController.class);
	}

	@Override
	public void configEngine(Engine me) {
		// TODO Auto-generated method stub

	}

	@Override
	public void configPlugin(Plugins me) {
		// TODO Auto-generated method stub
		C3p0Plugin c3p0Plugin = new C3p0Plugin(PropKit.get("jdbcUrl"),
				PropKit.get("user"), PropKit.get("password"));
		me.add(c3p0Plugin);

		ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(
				c3p0Plugin); 
		activeRecordPlugin.addMapping("blog", Blog.class);
		me.add(activeRecordPlugin);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new GlobalInterceptor());
		me.add(new GlobalInterceptor1());
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new HtmlHandler());
		me.add(new FreeMarkerHandler());
	}

	@Override
	public void afterJFinalStart() {
		System.out.println("JFinal启动完成    启用调度线程");
	}
	
	@Override
	public void beforeJFinalStop() {
		System.out.println("JFinal将停止    结束调度线程");
	}
	
	public static void main(String[] args) {
		JFinal.start("WebRoot", 80, "/", 5);
	}

}
